package com.ybfenpan.controller.member.mall;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ybfenpan.model.mall.MyAddress;
import com.ybfenpan.service.mall.MyAddressService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ybfenpan.controller.core.BaseController;
import com.ybfenpan.model.Member;

import com.ybfenpan.taglib.pagetag.Pagination;
import com.ybfenpan.utils.ContextUtil;
import com.ybfenpan.utils.CryptUtil;
import com.ybfenpan.utils.MapUtil;

/**
 * Created by Administrator on 2016/3/25.
 */
@Controller
public class MyAddressController extends BaseController {
	@Resource
	private MyAddressService myAddressService;


	@RequestMapping("member/mall/myAddressList.shtml")
	public String logEleList(HttpServletRequest request) throws Exception {
		HashMap formMap = getParameterMap(request);

		Member member = ContextUtil.getMember();
		formMap.put("memberLoginName", member.getLoginName());
		formMap.put("pagination.orderBy", " a.id desc"); // 排列
		Pagination page = myAddressService.queryForPageList("myAddressForPage",
				formMap);
		request.setAttribute(Pagination.pageBeanName, page);
		return "member/mall/myAddressList";
	}

	@RequestMapping("member/mall/myAddressAddShow.shtml")
	public String myAddressAdd(HttpServletRequest request) throws Exception {

//		List<ProvinceVo> listProvinceVo = ParamMall.listProvinceVo;
//		request.setAttribute("listProvinceVo", listProvinceVo);
		return "member/mall/myAddressAdd";
	}

	@RequestMapping("member/mall/myAddressAdd.shtml") 
	public String myAddressAdd(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Member member = ContextUtil.getMember();
		Map formMap = getParameterMap(request);
		boolean result = true;
		String twoPassword = MapUtil.getString(formMap, "twoPassword");
		// 标题
		String personName = MapUtil.getString(formMap, "personName");
		String tel = MapUtil.getString(formMap, "tel");
		String code = MapUtil.getString(formMap, "code");
		String address = MapUtil.getString(formMap, "address");
		String province = MapUtil.getString(formMap, "province");

		String pwd = CryptUtil.getInstance().encrypt2String(twoPassword,
				CryptUtil.keyStr);
		List<MyAddress> list=myAddressService.saveAddress(member.getId());
		if (list!=null&&list.size()>0) {
			setMessage(model, "只能添加一条收货地址，如有需要请修改您的地址！");
			result = false;
		}
		
		if (!member.getTwoPassword().equals(pwd)) {

			setMessage(model, "安全密码不正确。");
			result = false;
		
		}
		if (personName == null || personName.length() < 1) {

			setMessage(model, "收货人姓名没有填写！");
			result = false;
		}

		if (code == null || code.length() < 1) {

			setMessage(model, "邮政编码没有填写！");
			result = false;
		}
		if (address == null || address.length() < 1) {

			setMessage(model, "详细地址没有填写！");
			result = false;
		}
		if (province == null || province.length() < 1) {

			setMessage(model, "国家地址没有填写！");
			result = false;
		} else {
			if (province.equals("中国大陆/China")) {
				if (tel == null
						|| tel.replaceAll(" ", "").length() != 11
						|| !tel.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$")) {
					setMessage(model, "手机号码必须是11位数的数字！");
					result = false;

				}
			} else {
				if (tel == null || tel.length() < 1) {
					setMessage(model, "手机号码没有填写没有填写！");
					result = false;

				}
			}
		}

		if (!result) {

			request.setAttribute("member", member);

			return "member/mall/myAddressAdd";
		}

		if (result) {
			MyAddress ma = new MyAddress();
			ma.setTel(tel);
			ma.setPersonName(personName);
			ma.setAddress(address);
			ma.setMemberId(member.getId());
			ma.setCode(code);

			ma.setProvince(province);

			this.myAddressService.saveOrUpdate(ma);
			return "redirect:myAddressList.shtml";
		}

		return "redirect:myAddressList.shtml";
	}

	/**
	 * 未报单删除
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("member/mall/myAddressDelete.shtml")
	public String myAddressDelete(HttpServletRequest request) throws Exception {
		Map formMap = getParameterMap(request);
		Integer id = MapUtil.getIntValue(formMap, "id");
		System.out.println("进入：" + id);
		// 删除的代理商
		MyAddress one = myAddressService.getByKey(id);
		// 登陆的代理商
		Member member = ContextUtil.getMember();
		if (one != null) {
			if (one.getMemberId() == member.getId()) {
				myAddressService.deleteById(id);
			}

		}
		return "redirect:myAddressList.shtml";

	}

	/**
	 * 未报单删除
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("member/mall/myAddressUpdateShow.shtml")
	public String myAddressUpdateShow(HttpServletRequest request)
			throws Exception {
		Map formMap = getParameterMap(request);
		Integer id = MapUtil.getIntValue(formMap, "id");
		// 删除的代理商
		MyAddress myAddress = myAddressService.getByKey(id);
		// 登陆的代理商
		Member member = ContextUtil.getMember();
		request.setAttribute("member", member);
		if (myAddress != null) {
			if (myAddress.getMemberId() == member.getId()) {

				request.setAttribute("myAddress", myAddress);

//				List<ProvinceVo> listProvinceVo = ParamMall.listProvinceVo;
//				request.setAttribute("listProvinceVo", listProvinceVo);
//
//				List<City> listCityVo = ParamMall.listCity;
//				request.setAttribute("listCityVo", listCityVo);
//
//				List<Area> listArea = ParamMall.listArea;
//				request.setAttribute("listArea", listArea);

			}

		}
		return "member/mall/myAddressUpdate";

	}

	@RequestMapping("member/mall/myAddressUpdate.shtml")
	public String myAddressUpdate(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Member member = ContextUtil.getMember();
		Map formMap = getParameterMap(request);
		Integer id = MapUtil.getIntValue(formMap, "id");
		boolean result = true;
		if (id > 0) {

			String twoPassword = MapUtil.getString(formMap, "twoPassword");
			// 标题
			String personName = MapUtil.getString(formMap, "personName");
			String tel = MapUtil.getString(formMap, "tel");
			String code = MapUtil.getString(formMap, "code");
			String address = MapUtil.getString(formMap, "address");
			String province = MapUtil.getString(formMap, "province");

			String language = MapUtil.getString(formMap, "language");

			String pwd = CryptUtil.getInstance().encrypt2String(twoPassword,
					CryptUtil.keyStr);
			if (!member.getTwoPassword().equals(pwd)) {

				setMessage(model, "安全密码不正确。");
				result = false;
			
			}
			if (personName == null || personName.length() < 1) {

				setMessage(model, "收货人姓名没有填写！");
				result = false;
			}

			if (code == null || code.length() < 1) {

				setMessage(model, "邮政编码没有填写！");
				result = false;
			}
			if (address == null || address.length() < 1) {

				setMessage(model, "详细地址没有填写！");
				result = false;
			}
			if (province == null || province.length() < 1) {

				setMessage(model, "国家地址没有填写！");
				result = false;
			} else {
				if (province.equals("中国大陆/China")) {
					if (tel == null
							|| tel.replaceAll(" ", "").length() != 11
							|| !tel.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$")) {
						setMessage(model, "手机号码必须是11位数的数字！");
						result = false;

					}
				} else {
					if (tel == null || tel.length() < 1) {
						setMessage(model, "手机号码没有填写没有填写！");
						result = false;

					}
				}
			}
			

			MyAddress ma = this.myAddressService.getByKey(id);
			if (ma.getMemberId() != member.getId()) {
				setMessage(model, "收货地址不正确！");
				result = false;

			}
			
			if (!result) {
				
				request.setAttribute("member", member);
				return "member/mall/myAddressUpdate";
			}
			
			if (result) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", id);
				map.put("tel", tel);
				map.put("personName", personName);
				map.put("address", address);
				map.put("code", code);
				map.put("province", province);

				map.put("tel", tel);

				this.myAddressService.update(map);
				return "redirect:myAddressList.shtml";

			}

		}
		// 删除的代理商
		MyAddress myAddress = myAddressService.getByKey(id);
		// 登陆的代理商

		if (myAddress != null) {
			if (myAddress.getMemberId() == member.getId()) {

				request.setAttribute("myAddress", myAddress);

//				List<ProvinceVo> listProvinceVo = ParamMall.listProvinceVo;
//				request.setAttribute("listProvinceVo", listProvinceVo);
//
//				List<City> listCityVo = ParamMall.listCity;
//				request.setAttribute("listCityVo", listCityVo);
//
//				List<Area> listArea = ParamMall.listArea;
//				request.setAttribute("listArea", listArea);

			}

		}
		
		return "redirect:myAddressList.shtml";
	}


}
